package com.song.user.util;

import com.alibaba.fastjson.JSONObject;
import com.song.common.util.StringUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class RedisUtil {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * hash获取对象value
     * @param key
     * @param hasKey
     * @param tClass
     * @param <T>
     * @return
     */
    public <T> T hashGet(String key, Object hasKey, Class<T> tClass){
        Object obj = redisTemplate.opsForHash().get(key, hasKey);
        if (ObjectUtils.isNotEmpty(obj)){
            return JSONObject.parseObject(obj.toString(), tClass);
        }
        return null;
    }

    /**
     * hash获取字符串value
     * @param key
     * @param hasKey
     * @return
     */
    public String hashGetString(String key, Object hasKey){
        Object obj = redisTemplate.opsForHash().get(key, hasKey);
        if (ObjectUtils.isNotEmpty(obj)){
            return obj.toString();
        }
        return "";
    }

    /**
     * hash批量获取对象value
     * @param key
     * @param hashKeys
     * @param tClass
     * @param <T>
     * @return
     */
    public <T> List<T> multiGet(String key, Collection<Object> hashKeys, Class<T> tClass){
        List<T> list = new ArrayList<>();
        List<Object> objects = redisTemplate.opsForHash().multiGet(key, hashKeys);
        if (!CollectionUtils.isEmpty(objects)){
            list = objects.stream().map(v -> JSONObject.parseObject(v.toString(), tClass)).collect(Collectors.toList());
        }
        return list;
    }

    /**
     * hash批量获取字符串value
     * @param key
     * @param hashKeys
     * @return
     */
    public List<String> multiGetString(String key, Collection<Object> hashKeys){
        List<String> list = new ArrayList<>();
        List<Object> objects = redisTemplate.opsForHash().multiGet(key, hashKeys);
        if (!CollectionUtils.isEmpty(objects)){
            list = objects.stream().map(v -> v.toString()).collect(Collectors.toList());
        }
        return list;
    }

    /**
     * hash获取所有对象value
     * @param key
     * @param tClass
     * @param <T>
     * @return
     */
    public <T> List<T> hashValues(String key, Class<T> tClass){
        List<T> list = new ArrayList<>();
        List<Object> values = redisTemplate.opsForHash().values(key);
        if (!CollectionUtils.isEmpty(values)){
            list = values.stream().map(v -> JSONObject.parseObject(v.toString(), tClass)).collect(Collectors.toList());
        }
        return list;
    }

    /**
     * hash获取所有字符串value
     * @param key
     * @return
     */
    public List<String> hashStringValues(String key) {
        List<String> list = new ArrayList<>();
        List<Object> values = redisTemplate.opsForHash().values(key);
        if (!CollectionUtils.isEmpty(values)){
            list = values.stream().map(v -> v.toString()).collect(Collectors.toList());
        }
        return list;
    }
}
